package com.ccg.plat.presentation.ui.feature2.view

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Button
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.ccg.plat.data.local.cache.UserInfoCache
import com.ccg.plat.presentation.common.util.copyStr
import com.ccg.plat.presentation.ui.feature2.viewmodel.MainViewModel
import com.ccg.plat.presentation.ui.feature9.view.AdminActivity
import com.ccg.plat.ui.theme.AndroidVideoPlayerClientTheme
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


/**
 * @author : C4_雍和
 * 描述 :
 * 主要功能 :
 * 维护人员 : C4_雍和
 * date : 2025/8/9 8:34
 */
class MainActivity : ComponentActivity() {
    private val context = this
    private val viewModel by lazy { ViewModelProvider(this)[MainViewModel::class.java] }
    var isShowfdsa = 10
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lifecycleScope.launch(Dispatchers.IO) {
            viewModel.initData()
        }
        enableEdgeToEdge()
        setContent {
            AndroidVideoPlayerClientTheme {
                var currentNavigationIndex by remember { mutableIntStateOf(0) }
                Scaffold(modifier = Modifier.fillMaxSize(), bottomBar = {
                    NavigationBar(containerColor = Color.White) {
                        viewModel.uiState.navigationItems.forEachIndexed { index, navigationItem ->
                            NavigationBarItem(selected = currentNavigationIndex == index, onClick = {
                                currentNavigationIndex = index
                            }, icon = {
                                Image(painter = painterResource(id = navigationItem.selectedId), contentDescription = null)
                            }, label = {
                                Text(text = navigationItem.title)
                            }, alwaysShowLabel = false, colors = NavigationBarItemDefaults.colors(selectedIconColor = Color(0xff149ee7), selectedTextColor = Color(0xff149ee7), unselectedIconColor = Color(0xff999999), unselectedTextColor = Color(0xff999999)))
                        }
                    }
                }) { innerPadding ->
                    when (currentNavigationIndex) {
                        0 -> {
                            MainUI(modifier = Modifier.padding(innerPadding))
                        }

                        1 -> {
                            MyUI(modifier = Modifier.padding(innerPadding))
                        }
                    }
                }
            }
        }
    }

    @Composable
    fun MainUI(modifier: Modifier = Modifier) {
        LazyColumn(modifier = modifier, horizontalAlignment = Alignment.CenterHorizontally) {
            item {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .wrapContentHeight()
                        .clickable {
                            lifecycleScope.launch(Dispatchers.IO) {
                                viewModel.newsChannel.send(MainViewModel.ContentIntent.GoRoomListActivity(context))
                            }
                        }) {
                    Text(text = "视频播放列表", modifier = Modifier.padding(start = 20.dp, top = 15.dp, bottom = 15.dp), fontSize = 20.sp)
                    HorizontalDivider(thickness = 1.dp)
                }
            }
            item {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .wrapContentHeight()
                        .clickable {
                            lifecycleScope.launch(Dispatchers.IO) {
                                viewModel.newsChannel.send(MainViewModel.ContentIntent.GoCollectActivity(context))
                            }
                        }) {
                    Text(text = "收藏夹", modifier = Modifier.padding(start = 20.dp, top = 15.dp, bottom = 15.dp), fontSize = 20.sp)
                    HorizontalDivider(thickness = 1.dp)
                }
            }
        }
    }

    @SuppressLint("UnrememberedMutableInteractionSource")
    @Composable
    fun MyUI(modifier: Modifier = Modifier) {
        var checkedState by remember { mutableStateOf(UserInfoCache.isDefault()) }
        var pagerState by remember { mutableStateOf(UserInfoCache.isPagerState()) }
        Column(
            modifier = modifier
                .fillMaxSize()
                .padding(horizontal = 12.dp)
        ) {
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "设置默认播放器")
                Button(onClick = {
                    checkedState = !checkedState
                    UserInfoCache.setVideoDefault(checkedState)
                }) {
                    Text(text = if (checkedState) "播放器1" else "播放器2")
                }
            }
            Spacer(modifier = Modifier.height(12.dp))
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "播放页横竖屏设置")
                Button(onClick = {
                    pagerState = !pagerState
                    UserInfoCache.setPagerState(pagerState)
                }) {
                    Text(
                        text = if (pagerState) {
                            "横屏"
                        } else {
                            "竖屏"
                        }
                    )
                }
            }
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(50.dp)
            ) {
                var isShow by remember { mutableStateOf("免费用户") }
                LaunchedEffect(UserInfoCache.isVip()) {
                    isShow = if (UserInfoCache.isVip()) {
                        "VIP"
                    } else {
                        "免费用户"
                    }
                }
                Text(text = "您当前的状态:   $isShow")
            }
            Text(text = "免费用户每天能看10条视频,VIP用户无限制")
            Spacer(modifier = Modifier.height(12.dp))
            Text(text = "购买VIP流程:微信或QQ任选")
            Spacer(modifier = Modifier.height(12.dp))
            Text(text = "1点击QQ群号,就能复制群号,然后去加群")
            Spacer(modifier = Modifier.height(12.dp))
            Text(text = "2点击微信号,就能复制,然后去微信里添加好友")
            Spacer(modifier = Modifier.height(12.dp))
            Text(text = "开通VIP需要提供用户名,点击用户名就能复制")
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(40.dp)
                    .clickable {
                        Toast.makeText(context, "QQ群号复制成功", Toast.LENGTH_SHORT).show()
                        context.copyStr("463208733")
                    }, verticalAlignment = Alignment.CenterVertically
            ) {
                Text(text = "QQ群号:")
                Spacer(modifier = Modifier.width(12.dp))
                Text(text = "463208733")
            }
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(40.dp)
                    .clickable {
                        Toast.makeText(context, "微信号复制成功", Toast.LENGTH_SHORT).show()
                        context.copyStr("957493412")
                    }, verticalAlignment = Alignment.CenterVertically
            ) {
                Text(text = "微信号:")
                Spacer(modifier = Modifier.width(12.dp))
                Text(text = "957493412")
            }
            Row(modifier = Modifier
                .fillMaxWidth()
                .height(40.dp)
                .clickable {
                    Toast.makeText(context, "用户名复制成功", Toast.LENGTH_SHORT).show()
                    context.copyStr(UserInfoCache.getJpushId())
                }, verticalAlignment = Alignment.CenterVertically) {
                Text(text = "用户名:")
                Spacer(modifier = Modifier.width(12.dp))
                Text(text = "" + UserInfoCache.getJpushId())
            }
            Spacer(modifier = Modifier.height(12.dp))
            Text(text = "注意: 软件卸载VIP就会消失,切记没事别卸载")
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(50.dp)
                    .clickable(interactionSource = MutableInteractionSource(), indication = null) {
                        if (isShowfdsa > 0) {
                            isShowfdsa -= 1
                        } else {
                            startActivity(Intent(context, AdminActivity::class.java))
                        }
                    }) {}
        }
    }
}